package com.wwj.admin.system.controller;

import com.wwj.core.system.entity.Menu;
import com.wwj.core.system.service.IMenuService;
import com.wwj.framework.controller.BaseController;
import com.wwj.framework.utils.ReturnData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by xya on 16-10-25.
 */
@Controller
@RequestMapping("/system/menu")
public class MenuController extends BaseController {

    @Resource
    private IMenuService menuService;

    /**
     * 菜单树结构json(bootstrap-treeView控件)
     *
     * @return ReturnData对象
     */
    @RequestMapping(value = "/tree.json", method = RequestMethod.GET)
    @ResponseBody
    public ReturnData menuTree() {
        try {
            List<Menu> menus = menuService.findAllMenu();
            List<Map<String,Object>> menusTree = Menu.convert2TreeView(menus);
            return new ReturnData().success(menusTree);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new ReturnData().failure(e.getMessage());
        }
    }
}
